/**
 * <p>Title: ListConverter.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2009/11/18 0:10:18
 * @version 1.0
 */
package net.donotes.util.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import ognl.DefaultTypeConverter;

/**
 * @author WangJun
 *
 */
public class ListConverter extends DefaultTypeConverter {
	@SuppressWarnings("unchecked")
	@Override
	public Object convertValue(Map map, Object obj, Class toType) {
		if (toType == List.class) {
			String str = ((String[]) obj)[0];
			return StringToList(str.trim());
		} else if (toType == String.class) {
			List<String> list = (List<String>) obj;
			return join(list);
		}
		return null;
	}

	private List<String> StringToList(String str) {
		String[] strArray = str.split(" ");
		List<String> list = new ArrayList<String>();
		for (String s : strArray) {
			list.add(s);
		}
		return list;
	}
	
	private String join(List<String> list) {
		StringBuilder sb = new StringBuilder("");
		for (String s : list) {
			sb.append(s);
			sb.append(" ");
		}
		return sb.toString().trim();
	}
}
